package com.suarte.webapp.action;

import com.suarte.core.DeliveryTime;
import com.suarte.core.Guarantee;

import java.io.Serializable;

import org.appfuse.service.GenericManager;

/**
 * Created by IntelliJ IDEA.
 * User: Ggutierrez
 * Date: Mar 13, 2013
 * Time: 7:12:18 PM
 */
public class DeliveryTimeForm extends BasePage implements Serializable {
    private GenericManager<DeliveryTime, Long> deliveryTimeManager;
    private DeliveryTime deliveryTime  = new DeliveryTime();
    private Long id;

    public void setDeliveryTimeManager(GenericManager<DeliveryTime, Long> deliveryTimeManager) {
        this.deliveryTimeManager = deliveryTimeManager;
    }

    public DeliveryTime getDeliveryTime() {
        return deliveryTime;
    }

    public void setDeliveryTime(DeliveryTime deliveryTime) {
        this.deliveryTime = deliveryTime;
    }   

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        deliveryTimeManager.remove(deliveryTime.getId());
        addMessage("guarantee.deleted");

        return "list";
    }

    public String edit() {
        if (id != null) {
            deliveryTime = deliveryTimeManager.get(id);
        } else {
            deliveryTime = new DeliveryTime();
        }

        return "edit";
    }

    public String add() {
        deliveryTime = new DeliveryTime();

        return "add";
    }

    public String save() {
        boolean isNew = (deliveryTime.getId() == null);
        deliveryTimeManager.save(deliveryTime);

        String key = (isNew) ? "deliveryTime.added" : "deliveryTime.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
            return "edit";
        }
    }
}
